#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 2e3 + 10, M = 3e3 + 10;
LL n, m, ans;
int p[N];
struct node
{
	int a, b, w;
}S[M];
int cmp(node a, node b)
{
	return a.w < b.w;
}
int find(int x)
{
	if (x != p[x]) {
		p[x] = find(p[x]);
	}
	return p[x];
}
void kruskal()
{
	sort(S, S + m, cmp);
	for (int i = 1; i <= n; i++)p[i] = i;
	for (int i = 0; i < m; i++) {
		int a = S[i].a;
		int b = S[i].b;
		int a1 = find(a);
		int b1 = find(b);
		if (a1 != b1) {
			ans += S[i].w;
			p[a1] = b1;
		}
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int a, b, w;
		cin >> a >> b >> w;
		S[i] = { a,b,w };
	}
	kruskal();
	cout << ans << endl;
	return 0;
}